home *** CD-ROM | disk | FTP | other *** search
/ Visual Cafe 3 / Visual Cafe 3.ISO / Vcafe / Sample.bin / Blink.java < prev    next >
Text File  |  1998-09-15  |  4KB  |  120 lines

  1. /*
  2.  * @(#)Blink.java    1.4 97/02/05
  3.  *
  4.  * Copyright (c) 1997 Sun Microsystems, Inc. All Rights Reserved.
  5.  *
  6.  * Sun grants you ("Licensee") a non-exclusive, royalty free, license to use,
  7.  * modify and redistribute this software in source and binary code form,
  8.  * provided that i) this copyright notice and license appear on all copies of
  9.  * the software; and ii) Licensee does not utilize the software in a manner
  10.  * which is disparaging to Sun.
  11.  *
  12.  * This software is provided "AS IS," without a warranty of any kind. ALL
  13.  * EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND WARRANTIES, INCLUDING ANY
  14.  * IMPLIED WARRANTY OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE OR
  15.  * NON-INFRINGEMENT, ARE HEREBY EXCLUDED. SUN AND ITS LICENSORS SHALL NOT BE
  16.  * LIABLE FOR ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING
  17.  * OR DISTRIBUTING THE SOFTWARE OR ITS DERIVATIVES. IN NO EVENT WILL SUN OR ITS
  18.  * LICENSORS BE LIABLE FOR ANY LOST REVENUE, PROFIT OR DATA, OR FOR DIRECT,
  19.  * INDIRECT, SPECIAL, CONSEQUENTIAL, INCIDENTAL OR PUNITIVE DAMAGES, HOWEVER
  20.  * CAUSED AND REGARDLESS OF THE THEORY OF LIABILITY, ARISING OUT OF THE USE OF
  21.  * OR INABILITY TO USE SOFTWARE, EVEN IF SUN HAS BEEN ADVISED OF THE
  22.  * POSSIBILITY OF SUCH DAMAGES.
  23.  *
  24.  * This software is not designed or intended for use in on-line control of
  25.  * aircraft, air traffic, aircraft navigation or aircraft communications; or in
  26.  * the design, construction, operation or maintenance of any nuclear
  27.  * facility. Licensee represents and warrants that it will not use or
  28.  * redistribute the Software for such purposes.
  29.  */
  30.  
  31. import java.awt.*;
  32. import java.util.StringTokenizer;
  33.  
  34. /**
  35.  * I love blinking things.
  36.  *
  37.  * @author Arthur van Hoff
  38.  * @modified 04/24/96 Jim Hagen use getBackground
  39.  * @modified 02/05/98 Mike McCloskey removed use of deprecated methods
  40.  */
  41.  
  42. public class Blink extends java.applet.Applet implements Runnable
  43. {
  44.     Thread blinker = null;    // The thread that displays images 
  45.     String labelString;       // The label for the window              
  46.     int delay;                // the delay time between blinks
  47.  
  48.     public void init() {
  49.     String blinkFrequency = getParameter("speed");
  50.     delay = (blinkFrequency == null) ? 400 :
  51.             (1000 / Integer.parseInt(blinkFrequency));
  52.     labelString = getParameter("lbl");
  53.     if (labelString == null)
  54.             labelString = "Blink";
  55.         Font font = new java.awt.Font("TimesRoman", Font.PLAIN, 24);
  56.     setFont(font);
  57.     }
  58.     
  59.     public void paint(Graphics g) {
  60.         int fontSize = g.getFont().getSize();
  61.     int x = 0, y = fontSize, space;
  62.     int red =   (int)( 50 * Math.random());
  63.     int green = (int)( 50 * Math.random());
  64.     int blue =  (int)(256 * Math.random());
  65.     Dimension d = getSize();
  66.         g.setColor(Color.black);
  67.     FontMetrics fm = g.getFontMetrics();
  68.     space = fm.stringWidth(" ");
  69.     for (StringTokenizer t = new StringTokenizer(labelString); t.hasMoreTokens();) {
  70.         String word = t.nextToken();
  71.         int w = fm.stringWidth(word) + space;
  72.         if (x + w > d.width) {
  73.         x = 0;
  74.         y += fontSize;
  75.         }
  76.         if (Math.random() < 0.5)
  77.         g.setColor(new java.awt.Color((red + y*30) % 256, (green + x/3) % 256, blue));
  78.         else
  79.                 g.setColor(getBackground());
  80.         g.drawString(word, x, y);
  81.         x += w;
  82.     }
  83.     }
  84.  
  85.     public void start() {
  86.     blinker = new Thread(this);
  87.     blinker.start();
  88.     }
  89.  
  90.     public void stop() {
  91.     blinker = null;
  92.     }
  93.  
  94.     public void run() {
  95.         Thread me = Thread.currentThread();
  96.     while (blinker == me) {
  97.             try { 
  98.                 Thread.currentThread().sleep(delay);
  99.             } 
  100.             catch (InterruptedException e) {
  101.             }
  102.         repaint();
  103.     }
  104.     }
  105.  
  106.   public String getAppletInfo() {
  107.       return "Title: Blinker\nAuthor: Arthur van Hoff\nDisplays multicolored blinking text.";
  108.   }  
  109.   
  110.   public String[][] getParameterInfo() {
  111.       String pinfo[][] = {
  112.           {"speed", "string", "The blink frequency"},
  113.           {"lbl", "string", "The text to blink."},
  114.       };
  115.       return pinfo;
  116.   }
  117.  
  118. }
  119.  
  120.